<template>
  <div>
     <van-search class="Search_box" v-model="searchTitle" show-action placeholder="服务名称/联系电话/联系人" @search="search" @clear="onCancel">
      <template #action>
        <div @click="search">搜索</div>
      </template>
    </van-search>
    <div class="areaselect" id="areaselect">
    <van-dropdown-menu :overlay="false" get-container="#areaselect">
      <van-dropdown-item v-model="regionValue" :options="region" @change="bara" />
      <van-dropdown-item v-model="streetValue" :options="street" @change="bara2" />
    </van-dropdown-menu>
  </div>
  </div>
 
</template>
<script>
export default {
  data() {
    return {
      searchTitle: "",
      regionValue: "",
      streetValue: "",
      area: {},
      columns: [
        {
          values: [],
          valuesorigin: [],
          className: "column1"
        },
        {
          values: [],
          valuesorigin: [],
          className: "column2"
        }
      ],
      region: [
        {
          text: "全部街镇",
          value: ""
        }
      ], //活动区域
      street: [
        {
          text: "全部村社区",
          value: ""
        }
      ]
    };
  },
  props: {
    listData: {}
  },
  mounted() {
    this.zoneAction(() => {
      this.rquestPostTemperature();
    });
  },
  methods: {
    search() {
      this.$emit("load", this.regionValue, this.searchTitle);
    },
    onCancel(){
      this.searchTitle = '';
      this.$emit("loadClearsearch", this.regionValue, this.searchTitle);
    },
    bara() {
      this.$emit("load", this.regionValue, this.searchTitle);
      this.$emit("loadClearsearch", this.regionValue, this.searchTitle);
      this.street[0].value = this.regionValue;
      this.streetValue = this.regionValue;
      let id = "";
      this.columns[0].valuesorigin.forEach(e => {
        if (e.dataAreaCode == this.regionValue) {
          id = e.id;
        }
      });
      let ab = [
        {
          text: "全部村社区",
          value: this.regionValue
        }
      ];
      this.area.Committe.forEach(e => {
        if (e.parentId == id) {
          ab.push({
            text: e.name,
            value: e.dataAreaCode
          });
        }
      });
      this.street = ab;
    },
    bara2() {
      this.$emit("load", this.streetValue, this.searchTitle);
      this.$emit("loadClearsearch", this.regionValue, this.searchTitle);
    },
    rquestPostTemperature() {
      this.$toast.loading({
        message: "区划加载中...",
        forbidClick: true
      });
      this.post("region/regionTree")
        .then(res => {
          this.$toast.clear();
          this.area = res.map;
          this.area.street.forEach(element => {
            if (element.parentId === this.area.area[0].id) {
              this.columns[0].valuesorigin.push(element);
              this.columns[0].values.push(element.name);
            }
          });
          let ab = [
            {
              text: "全部村社区",
              value: this.regionValue
            }
          ];
          let streetValue = "";
          let abparentId = "";
          this.area.Committe.forEach(element => {
            if (element.parentId === this.columns[0].valuesorigin[0].id) {
              this.columns[1].valuesorigin.push(element);
              this.columns[1].values.push(element.name);
            }
            if (
              element.dataAreaCode == sessionStorage.getItem("dataAreaCode")
            ) {
              abparentId = element.parentId;
              streetValue = element.dataAreaCode;
            }
          });
          this.area.street.forEach(e => {
            if (abparentId === e.id) {
              this.regionValue = e.dataAreaCode;
              ab[0].value = e.dataAreaCode;
              this.area.Committe.forEach(ep => {
                if (ep.parentId == e.id) {
                  ab.push({
                    text: ep.name,
                    value: ep.dataAreaCode
                  });
                }
              });
            }
          });
          this.street = ab;
          this.streetValue = streetValue;
        })
        .catch(err => {
          this.$toast.clear();
          console.log("异常", err);
        });
    },
    zoneAction(cb) {
      this.post("community_activity/regiontype")
        .then(r => {
          r.list.forEach(element => {
            if (this.region) {
              this.region.push({
                text: element.regionName,
                value: element.regionCode
              });
            } else {
              this.region.push({
                text: "活动区域",
                value: "0"
              });
            }
            this.region.forEach(e => {
              if (e.value == sessionStorage.getItem("dataAreaCode")) {
                this.regionValue = e.value;
              }
            });
            cb && cb();
          });
        })
        .catch(err => {});
    }
  },
  destroyed() {}
};
</script>
<style lang="scss">
@import "~@/smobile.scss";
#areaselect {
  position: relative;
  .van-dropdown-item--down {
    top: 256.398px !important;
  }
}
.Search_box{
  position: absolute;
  top: 0;
  width: 100%;
  left: 0;
  overflow: hidden;
}
.van-search {
  width: 100%;
  padding: 10px 12px 10px 20px;
  .van-search__action {
    width: 10%;
    text-align: center;
    padding: 0;
    margin: 0 0 0 8px;
    border-radius: 30%;
    background-color: #1989fa;
    color:white;
  }
}
</style>